﻿using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tool.Helper.Dm;

namespace DmTools
{
    public partial class MagnifierForm : Form
    {
        Pen _pen;
        // 放大倍数
        int zoom = 10;
        int width = 21, height = 21; // 放大区域大小（原始尺寸）
        int displayWidth, displayHeight;
        public MagnifierForm()
        {
            InitializeComponent();
            _pen = new Pen(Color.Red);
            timer1.Interval = 200;
            timer1.Start();
            _dm = new DmHelperEx();
            var result = _dm.Init();
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            if (!result)
            {
                MessageBox.Show("DM初始化失败！");
            }
            displayWidth = width * zoom;
            displayHeight = height * zoom;

        }
        DmHelperEx _dm;
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawLine(_pen, 0, pictureBox1.Height / 2, pictureBox1.Width, pictureBox1.Height / 2);
            e.Graphics.DrawLine(_pen, pictureBox1.Width / 2, 0, pictureBox1.Width / 2, pictureBox1.Height);

        }
        Point _location = new Point();
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (Cursor.Position == _location)
            {
                return;
            }
            UpdateMagnifier(Cursor.Position);
            //int n = 20;
            //Bitmap bitmap = new Bitmap(pictureBox1.Width / n, pictureBox1.Height / n);
            //using (Graphics g = Graphics.FromImage(bitmap))
            //{
            //    g.CopyFromScreen(new Point(Cursor.Position.X - bitmap.Width / 2, Cursor.Position.Y - bitmap.Height / 2), new Point(0, 0), bitmap.Size);
            //}
            //pictureBox1.Image = bitmap;
            var str = _dm.GetColor(Cursor.Position.X, Cursor.Position.Y);
            pal_color.BackColor = ColorTranslator.FromHtml("#" + str);
            tbx_color.Text = str;
            tbx_location.Text = Cursor.Position.X + "," + Cursor.Position.Y;
            _location = Cursor.Position;
        }
        private void UpdateMagnifier(Point cursorPos)
        {
            // 创建新位图用于绘制像素块
            var _currentBitmap = new Bitmap(displayWidth, displayHeight);
            using (Graphics g = Graphics.FromImage(_currentBitmap))
            {
                // 清空背景
                g.Clear(Color.Black);
                // 计算实际屏幕区域
                int x = cursorPos.X - width / 2;
                int y = cursorPos.Y - height / 2;

                // 拷贝屏幕内容到临时缓冲区
                using (Bitmap screenCapture = new Bitmap(width, height))
                {
                    using (Graphics gScreen = Graphics.FromImage(screenCapture))
                    {
                        gScreen.CopyFromScreen(new Point(x, y), new Point(0, 0), new Size(width, height));
                    }
                    // 将每个像素放大为一个方块
                    for (int i = 0; i < width; i++)
                    {
                        for (int j = 0; j < height; j++)
                        {
                            Color pixelColor = screenCapture.GetPixel(i, j);
                            g.FillRectangle(new SolidBrush(pixelColor), i * zoom, j * zoom, zoom, zoom);
                        }
                    }
                }
            }
            pictureBox1.Image = _currentBitmap;

        }

        private void MagnifierForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            timer1.Stop();
            timer1.Dispose();
        }

        private void MagnifierForm_Load(object sender, EventArgs e)
        {

        }
    }
}
